package hui.com.service.impl;

import hui.com.model.res.MyResponse;
import hui.com.dao.WarehouseDao;
import hui.com.model.entity.WarehouseEntity;
import hui.com.model.req.RequestWarehouse;
import hui.com.model.res.ResponseWarehouse;
import hui.com.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WarehouseServiceImpl implements WarehouseService {
    @Autowired
    @Resource
    WarehouseDao warehouseDao;

    @Override
    public Object addStorageLocationList(RequestWarehouse warehouse) {
        //System.out.println(warehouse);

        // 客户要求新增的库位
        List<WarehouseEntity> warehouseEntityList = xyzList(warehouse);

        // 查询客户准备新增的库位，在实际库位里已存在的部分
        List<WarehouseEntity> getWarehouseEntityList = warehouseDao.getListStorageLocation(warehouseEntityList);

        //筛出可新增的库位
        List<WarehouseEntity> addWareList = warehouseEntityList.stream()
                .filter(w1 -> getWarehouseEntityList.stream()
                        .noneMatch(w2 -> (w2.getX().equals(w1.getX()) && w2.getY().equals(w1.getY())
                                && w2.getZ().equals(w1.getZ()) && w2.getCodeName().equals(w1.getCodeName())))

                ).collect(Collectors.toList());


        //第二种，筛选出不存在的库位，直接进行新增
        if (addWareList.isEmpty()) {
            if (!getWarehouseEntityList.isEmpty()) {
                return MyResponse.builder().code(201).data("需要新增的库位都已存在").build();
            } else {
                return MyResponse.builder().code(201).data("无新增参数").build();
            }
        } else {
            return MyResponse.builder().code(200).data("新增了:" + warehouseDao.addStorageLocationList(addWareList)).build();
        }
        // 第一种只要有一个库位实际存在就不允许新增
//        if (getWarehouseEntityList==null){
//            MyResponse.builder().code(200).data(warehouseDao.addStorageLocationList(warehouseEntityList)).build();
//        }else{
//          return MyResponse.builder().code(202).data(getWarehouseEntityList).msg("无法新增，需要新增的库位，部分或全部，在库中已存在").build();
//        }
//
//
    }

    @Override
    public String[] getCodeNameArray() {
        return warehouseDao.getCodeNameArray();
    }

    @Override
    public List<ResponseWarehouse> getListCodeNameStorageLocation(WarehouseEntity warehouse) {
        return warehouseDao.getListCodeNameStorageLocation(warehouse);
    }

    @Override
    public Integer[] getListCodeNameXYZStorageLocation(WarehouseEntity warehouse) {
        return warehouseDao.getListCodeNameXYZStorageLocation(warehouse);
    }

    private List<WarehouseEntity> xyzList(RequestWarehouse warehouse) {
        // 客户要求新增的库位
        List<WarehouseEntity> warehouseEntityList = new ArrayList<>();
        switch (warehouse.getType()) {
            case "listX":
                for (int i = warehouse.getMinY(); i <= warehouse.getMaxY(); i++) {
                    for (int j = warehouse.getMinZ(); j <= warehouse.getMaxZ(); j++) {
                        WarehouseEntity warehouseEntity = new WarehouseEntity();
                        warehouseEntity.setY(i);
                        warehouseEntity.setZ(j);
                        warehouseEntity.setX(warehouse.getX());
                        warehouseEntity.setCodeName(warehouse.getCodeName());
                        warehouseEntity.setIsHave(warehouse.getIsHave());
                        warehouseEntity.setState(warehouse.getState());
                        warehouseEntityList.add(warehouseEntity);
                    }
                }
                break;
            case "listY":
                for (int i = warehouse.getMinX(); i <= warehouse.getMaxX(); i++) {
                    for (int j = warehouse.getMinZ(); j <= warehouse.getMaxZ(); j++) {
                        WarehouseEntity warehouseEntity = new WarehouseEntity();
                        warehouseEntity.setX(i);
                        warehouseEntity.setZ(j);
                        warehouseEntity.setY(warehouse.getY());
                        warehouseEntity.setCodeName(warehouse.getCodeName());
                        warehouseEntity.setIsHave(warehouse.getIsHave());
                        warehouseEntity.setState(warehouse.getState());
                        warehouseEntityList.add(warehouseEntity);
                    }
                }
                break;
            case "listZ":
                for (int i = warehouse.getMinX(); i <= warehouse.getMaxX(); i++) {
                    for (int j = warehouse.getMinY(); j <= warehouse.getMaxY(); j++) {
                        WarehouseEntity warehouseEntity = new WarehouseEntity();
                        warehouseEntity.setX(i);
                        warehouseEntity.setY(j);
                        warehouseEntity.setZ(warehouse.getZ());
                        warehouseEntity.setCodeName(warehouse.getCodeName());
                        warehouseEntity.setIsHave(warehouse.getIsHave());
                        warehouseEntity.setState(warehouse.getState());
                        warehouseEntityList.add(warehouseEntity);
                    }
                }
                break;
            case "list":

                for (int i = warehouse.getMinX(); i <= warehouse.getMaxX(); i++) {
                    for (int j = warehouse.getMinY(); j <= warehouse.getMaxY(); j++) {
                        for (int n = warehouse.getMinZ();n<=warehouse.getMaxZ();n++) {
                            WarehouseEntity warehouseEntity = new WarehouseEntity();
                            warehouseEntity.setX(i);
                            warehouseEntity.setY(j);
                            warehouseEntity.setZ(n);
                            warehouseEntity.setCodeName(warehouse.getCodeName());
                            warehouseEntity.setIsHave(warehouse.getIsHave());
                            warehouseEntity.setState(warehouse.getState());
                            warehouseEntityList.add(warehouseEntity);
                        }
                    }
                }
                break;
            case "single":
                WarehouseEntity warehouseEntity = new WarehouseEntity();
                warehouseEntity.setX(warehouse.getX());
                warehouseEntity.setY(warehouse.getY());
                warehouseEntity.setZ(warehouse.getZ());
                warehouseEntity.setCodeName(warehouse.getCodeName());
                warehouseEntity.setIsHave(warehouse.getIsHave());
                warehouseEntity.setState(warehouse.getState());
                warehouseEntityList.add(warehouseEntity);
                break;

        }
        return warehouseEntityList;
    }
}
